/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package demo;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Image;
import java.text.NumberFormat;
import javax.swing.*;
import javax.swing.table.DefaultTableCellRenderer;

/**
 *
 * @author CodeBlue
 */
public class MyTableRenderer {
    
    public static class ColorRenderer extends DefaultTableCellRenderer {

        @Override
        public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
            setForeground(new Color(153, 0, 0));
            super.getTableCellRendererComponent(table, value, isSelected,
                    hasFocus, row, column);
            return this;
        }
    }
    
    public static class AlignRenderer extends DefaultTableCellRenderer {

        @Override
        public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
            // align center
            setHorizontalAlignment(SwingConstants.CENTER);
            // align left
            //setHorizontalAlignment(SwingConstants.LEFT);
            // align right
            //setHorizontalAlignment(SwingConstants.RIGHT);
            super.getTableCellRendererComponent(table, value, isSelected,
                    hasFocus, row, column);
            return this;
        }
    }
    
    public static class ImageRenderer extends DefaultTableCellRenderer {

        @Override
        public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
            boolean bool = Boolean.parseBoolean(value.toString());
            Image img = null;
            if(bool) {
                img = getToolkit().getImage(getClass().getResource("/images/male.png"));
            } else {
                img = getToolkit().getImage(getClass().getResource("/images/female.png"));
            }
            setSize(16, 16);
            setHorizontalAlignment(SwingConstants.CENTER);
            setIcon(new ImageIcon(img));
            super.getTableCellRendererComponent(table, "", isSelected,
                    hasFocus, row, column);
            return this;
        }
    }
    
    public static class CurrencyRenderer extends DefaultTableCellRenderer {

        @Override
        public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
            if ((value != null) && (value instanceof Number)) {
                Number numberValue = (Number) value;
                NumberFormat formater = NumberFormat.getCurrencyInstance();
                value = formater.format(numberValue.doubleValue());
            }
            setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
            super.getTableCellRendererComponent(table, value, isSelected,
                    hasFocus, row, column);
            return this;
        }
    }
    
    public static class OutputRenderer extends DefaultTableCellRenderer {

        @Override
        public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
            value = "Mr." + value;
            super.getTableCellRendererComponent(table, value, isSelected,
                    hasFocus, row, column);
            return this;
        }
    }
    
    public static class BgRenderer extends DefaultTableCellRenderer {

        @Override
        public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
            JPanel pn = new JPanel(new BorderLayout());
            JLabel lb = new JLabel();
            pn.add(lb, BorderLayout.CENTER);
            lb.setHorizontalAlignment(SwingConstants.CENTER);
            lb.setText(value.toString());
            lb.setForeground(Color.BLUE);
            pn.setBackground(Color.RED);
            
            super.getTableCellRendererComponent(table, value, isSelected,
                    hasFocus, row, column);
            return pn;
        }
    }
}
